﻿using gt.webapi.core.Entity.Client;
using gt.webapi.core.Filters.Authority;
using gt.webapi.core.Repositories;
using gt.webapi.core.Repositories.Impl;
using System;
using System.Collections.Generic;
using System.Web.Http;

namespace gt.webapi.core.Entity.Options
{
    public class AuthorityOption
    {
        private IAuthorityRepository m_authorityRepository = null;
        private AuthenticationFilterBase m_authenticationFilter = null;
        private AuthorizeFilterBase m_authorizeFilter = null;

        public AuthorityOption(List<AuthorityEntity> Authorities)
            : this(new MemoryAuthorityRepository())
        {
            if (Authorities == null) throw new ArgumentNullException("Authorities");
            this.m_authorityRepository.AddAuthorities(Authorities);
        }

        public AuthorityOption(IAuthorityRepository authorityRepository)
        {
            this.m_authorityRepository = authorityRepository ?? throw new ArgumentNullException("authorityRepository");
        }

        public IAuthorityRepository AuthorityRepository { get { return this.m_authorityRepository; } }
        public AuthenticationFilterBase AuthenticationFilter { get { return this.m_authenticationFilter; } }
        public AuthorizeAttribute AuthorizeFilter { get { return this.m_authorizeFilter; } }

        public AuthorityOption UsingBasicAuthentication()
        {
            return UsingAuthentication(new BasicAuthenticationFilter(this.m_authorityRepository), new DefaultAuthorizeAttribute(this.m_authorityRepository));
        }
        public AuthorityOption UsingAuthentication(AuthenticationFilterBase authenticationFilter, AuthorizeFilterBase authorizeFilter)
        {
            this.m_authenticationFilter = authenticationFilter;
            this.m_authorizeFilter = authorizeFilter;
            return this;
        }
    }
}
